package com.shermanzhou.freemarker;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateExceptionHandler;

import java.io.*;
import java.util.*;

/**
 * @author zhouchaoqun
 * @date 2018/11/22 上午11:34
 */
public class MapperGenTest {
    public static void main(String[] args) throws Exception{
        Configuration cfg = new Configuration(Configuration.VERSION_2_3_28);
        File templateDir = new File(FreemarkerTest.class.getClassLoader().getResource("templates").toURI());
        cfg.setDirectoryForTemplateLoading(templateDir);
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);

        List<String> columns = Arrays.asList("id","restaurant_code","restaurant_name","restaurant_type","province","city","res_id","aptitude_type","brand_code","brand_type","brand_name","license_company_name","license_attachment","license_end_date","license_open_date","food_license_company_name","food_license_attachment","food_license_end_date","is_license_match","license_not_match_attachment","is_special_invoice","special_invoice_begin_date","special_invoice","first_party_name","second_party_name","is_second_party_match","second_party_not_match_attachment","contract_attachment","contract_end_date","contract_clause_one","contract_clause_two","contract_clause_three","contract_clause_memo","invoice_sales_name","invoice_image","is_invoice_sales_match","invoice_sales_not_match_attachment","restaurant_image","menu_list","ticket_image","authorize_attachment","remark","license_expired","permit_expired","first_upload_status","farther_upload_status","data_update_status","status","gmt_modified","gmt_created");
        List<String> fields = Arrays.asList("id", "restaurantCode", "restaurantName", "restaurantType", "province", "city", "resId", "aptitudeType", "brandCode", "brandType", "brandName", "licenseCompanyName", "licenseAttachment", "licenseEndDate", "licenseOpenDate", "foodLicenseCompanyName", "foodLicenseAttachment", "foodLicenseEndDate", "isLicenseMatch", "licenseNotMatchAttachment", "isSpecialInvoice", "specialInvoiceBeginDate", "specialInvoice", "firstPartyName", "secondPartyName", "isSecondPartyMatch", "secondPartyNotMatchAttachment", "contractAttachment", "contractEndDate", "contractClauseOne", "contractClauseTwo", "contractClauseThree", "contractClauseMemo", "invoiceSalesName", "invoiceImage", "isInvoiceSalesMatch", "invoiceSalesNotMatchAttachment", "restaurantImage", "menuList", "ticketImage", "authorizeAttachment", "remark", "licenseExpired", "permitExpired", "firstUploadStatus", "fartherUploadStatus", "dataUpdateStatus", "status", "gmtModified", "gmtCreated");

        TableModel tableModel = new TableModel();
        tableModel.setTable("sync_res_prepared");
        tableModel.setColumns(columns);
        tableModel.setFields(fields);

        Template template = cfg.getTemplate("mapper.ftl");

        OutputStream outputStream = new FileOutputStream("demo-freemarker/src/main/java/mapper.xml");
        System.out.println(outputStream);
        Writer out = new OutputStreamWriter(outputStream);
        template.process(tableModel, out);
    }

    public static class TableModel {
        private String table;
        private List<String> columns;
        private List<String> fields;

        public String getTable() {
            return table;
        }

        public void setTable(String table) {
            this.table = table;
        }

        public List<String> getColumns() {
            return columns;
        }

        public void setColumns(List<String> columns) {
            this.columns = columns;
        }

        public List<String> getFields() {
            return fields;
        }

        public void setFields(List<String> fields) {
            this.fields = fields;
        }
    }
}
